<?php
namespace app\command;


use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use app\common\model\School;


class SchoolColumn extends Command
{
    protected function configure()
    {
        $this->setName('school_column')->setDescription('Your command description');
    }

    protected function execute(Input $input, Output $output)
    {
		
		Db::name('school')->field('id,college_name,province,city,district,college_category,college_property,edu_level,college_tags,is985,is211,is_dual_class')->chunk(100, function ($list) use(&$nums){
			$dataU = [];	
			foreach($list as $k=>$v){
				//$college_tags = preg_replace("/[\[\]]/","",$v['college_tags']);
				//$college_tag_arr = explode(',',$college_tags);
				//dump($college_tag_arr);die;
				$type_id = Db::name('school_type')->where('title_fu',$v['college_category'])->value('id');//综合、体育。。。
				if($v['college_property'] == '公办'){
					$school_xingzhi = 1;
				}elseif($v['college_property'] == '民办'){
					$school_xingzhi = 2;
				}else{
					$school_xingzhi = 3;
				}
				if($v['edu_level'] == '普通本科' || $v['edu_level'] == '本科'){
					$school_xingzhi2 = 1;
				}else{
					$school_xingzhi2 = 2;
				}
				
				$school_tese = [];
				if($v['is985'] == 1){
					$school_tese[] = 1;
				}
				if($v['is211'] == 1){
					$school_tese[] = 2;
				}
				if($v['is_dual_class'] == 1){
					$school_tese[] = 3;
				}
				if(!empty($school_tese)){
					Db::name('school_tese_data')->where('school_id',$v['id'])->delete();
					$school_tese = array_filter($school_tese);
					$dataTs = [];
					foreach ($school_tese as $val) {
						 $dataTs[] = ['tese_id' => $val, 'school_id' => $v['id']];
					}
					Db::name('school_tese_data')->insertAll($dataTs);
				}
				$province_id = Db::name('area')->where('level',1)->where('name',$v['province'])->value('id');
				$city_id = Db::name('area')->where('level',2)->where('name',$v['city'])->value('id');
				$xian_id = Db::name('area')->where('level',3)->where('name',$v['district'])->value('id');
					
				$dataU[$k] = ['id'=>$v['id'],'province_id'=>$province_id,'city_id'=>$city_id,'xian_id'=>$xian_id,'school_type'=>$type_id,'school_xingzhi'=>$school_xingzhi,'school_xingzhi2'=>$school_xingzhi2];
			}
			if(isset($dataU) && !empty($dataU)) {
				$obj = new School();
				$obj->saveAll($dataU);
			}
			$nums++;
			setLog('school_column','成功：'.$nums);
		});
		setLog('school_column','执行完毕');exit();
		
    }


}
